﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Data;
using LinkCard.Model;

namespace LinkCard.AdminCP.Deal
{
    public partial class category_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }
            if (!IsPostBack)
            {
                int nCategoryID = ConvertUtility.ToInt32(Request.QueryString["id"]);
                int nCategoryParentID = 0;
                int nCatePosition = 0;
                DataTable dt;


                dt = DataHelper.SelectTable("SELECT * FROM Category WHERE CategoryID = " + nCategoryID + "", false);
                if (dt.Rows.Count > 0)
                {

                    nCategoryParentID = ConvertUtility.ToInt32(dt.Rows[0]["ParentID"]);
                    CategoryName.Value = dt.Rows[0]["Name"].ToString();
                    if (ConvertUtility.ToBoolean(dt.Rows[0]["IsDisplay"])) Display.Checked = true;
                    CategoryLevel.Value = dt.Rows[0]["Level"].ToString();
                    CategoryText.Value = dt.Rows[0]["Text"].ToString();
                    nCatePosition = ConvertUtility.ToInt32(dt.Rows[0]["Position"].ToString());

                }
                dt = DataHelper.SelectTable("SELECT CategoryID, Text FROM Category Where Level < 2 ORDER BY Text", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Text";
                CategoryID.DataValueField = "CategoryID";
                CategoryID.DataBind();
                CategoryID.Items.Insert(0, "Nhóm mới");
                CategoryID.SelectedValue = nCategoryParentID.ToString();

                dt = DataHelper.SelectTable("Select count(*) as count from Category where Level = 1", false);
                int nCount = 1;
                if (dt.Rows.Count > 0) nCount = ConvertUtility.ToInt32(dt.Rows[0]["count"]);
                for (int i = 0; i < nCount; i++)
                {
                    CatePosition.Items.Insert(i, new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }
                CatePosition.SelectedValue = nCatePosition.ToString();


            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int nCategoryID = ConvertUtility.ToInt32(Request.QueryString["id"]);

                int nCategoryParentID = ConvertUtility.ToInt32(CategoryID.SelectedValue);
                int nCatePosition = ConvertUtility.ToInt32(CatePosition.SelectedValue);
                string sCategoryName = CategoryName.Value;
                string sCategoryText = "";


                int nDisplay = 0;
                if (Display.Checked) nDisplay = 1;
                int nCategoryLevel = 0;

                string sSQL = "";

                if (nCategoryParentID == 0)
                {
                    sCategoryText = sCategoryName;
                    nCategoryLevel = 1;
                }
                else
                {
                    sSQL = "select Text, Level from Category where CategoryID = " + nCategoryParentID;
                    DataTable dt = DataHelper.SelectTable(sSQL, false);
                    if (dt.Rows.Count > 0)
                    {
                        sCategoryText = dt.Rows[0]["Text"].ToString() + " >> " + sCategoryName;
                        nCategoryLevel = Convert.ToByte(dt.Rows[0]["Level"]) + 1;
                    }
                }
                //(Name, Text, ParentID, Level, IsDisplay)
                sSQL = "Update Category  Set ";
                sSQL += "Name= N'" + hash1way.SQLPatch(sCategoryName) + "',";
                sSQL += "Text = N'" + hash1way.SQLPatch(sCategoryText) + "',";
                sSQL += "ParentID = " + nCategoryParentID + ",";
                sSQL += "Level =" + nCategoryLevel + ",";
                sSQL += "Position =" + nCatePosition + ",";
                sSQL += "IsDisplay=" + nDisplay;
                sSQL += " where CategoryID=" + nCategoryID.ToString();

                DataHelper.ExecuteNonQuery(sSQL, true);

                Response.Redirect("category_list.aspx");
            }
            catch
            { }
        }
    }
}